xquery version "1.0";

module namespace onderbrekings="http://www.example.com/onderbrekings";
declare namespace exist = "http://exist.sourceforge.net/NS/exist"; 
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
declare namespace pm = "http://www.politicalmashup.nl";

 declare function onderbrekings:maakonderbrekersdata($collection){

 let $a := $collection/root/pm:proceedings/pm:topic/pm:speech
let $partijen := document('/db/projdb/partijen.xml')/partijen
  let $names := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker
return <onderbrekersdata> {
            for $x in $a
                let $prec := $x/preceding-sibling::pm:speech
                let $b := $x[not(./@pm:role = 'chair') and empty($prec[ends-with(./pm:p,'heeft het woord.')][position() =last()][contains(./pm:p,tokenize($x/@pm:speaker, "\s+")[2])])]
                where not(empty($b)) and not (empty($names[data($x/@pm:speaker) =./name])) and not (empty($names[data($prec[position()=last()]/@pm:speaker) =./name]))
                return <entry> 
                            <onderbreker>{
                                data($x/@pm:speaker)
                             }</onderbreker>
                             <ondmid>{
                              data($x/@pm:id)
                             }</ondmid>
                            
                             <ponderbreker>{
                             data($names[data($x/@pm:speaker)= ./name]/partij)
                             }</ponderbreker>
                             
                            
                             <onderbrokene>{data($prec[position()=last()]/@pm:speaker)
                             }</onderbrokene>
                             
                             <onderbmid>{
                             data($prec[position()=last()]/@pm:id)
                             }</onderbmid>
                             <ponderbrokene>{
                             data($names[data($prec[position()=last()]/@pm:speaker)= ./name]/partij)
                             }</ponderbrokene>
                             <aanhetwoord>
                             {data($prec[ends-with(./pm:p,'heeft het woord.')][position() =last()]/following-sibling::pm:speech[1]/@pm:speaker)}
                             </aanhetwoord>
                             <test>
                             {
                              
                             $prec[ends-with(./pm:p,'heeft het woord.')][position() =last()]/pm:p}
                          
                             
                             </test>
                       </entry>
}</onderbrekersdata>
 };
 
  declare function onderbrekings:onderpercperpartij($onderbrekersdata){
(: zichzelf toevoegen of niet ?:)
<perpartij>{
 let $a := $onderbrekersdata/entry
 for $i in distinct-values($a/ponderbreker)
   let $d := $a[data(./ponderbreker) = data($i)]
   return <entry><naam>{ $i }</naam><data>{
            for $j in distinct-values($a/ponderbrokene)
            let $g := $d[data(./ponderbrokene) = data($j)]
            return <entry>
                    <naam>{$j}</naam>
                    <perc>{round-half-to-even(100 *count($g) div count($d),2)}</perc>
                   </entry>}
            </data>
       </entry>
}</perpartij>
 };
 
 declare function onderbrekings:onderbrokendoorperpercpartij($onderbrekersdata){
     <perpartij>{
 let $a := $onderbrekersdata/entry
 for $i in distinct-values($a/ponderbrokene)
   let $d := $a[data(./ponderbrokene) = data($i)]
   return <entry><naam>{ $i }</naam><data>{
            for $j in distinct-values($a/ponderbreker)
            let $g := $d[data(./ponderbreker) = data($j)]
            return <entry>
                    <naam>{$j}</naam>
                    <perc>{round-half-to-even(100 *count($g) div count($d),2)}</perc>
                   </entry>}
            </data>
       </entry>
}</perpartij>
 };

 declare function onderbrekings:maakonderbrekerstotalen($onderbrekersdata){
 let $names := document('/db/projdb/lijst_van_sprekers_ge2009.xml')/speakers/speaker
 let $burg := document('/db/projdb/lijst_burgemeesters_2007.xml')/burgemeesters/tupel
 return <onderbrekerstotalen>{
    for $j in distinct-values($onderbrekersdata/entry/onderbreker)
        let $d := $onderbrekersdata/entry[./onderbreker = $j]
        let $g := $names[./name = $j]
        let $b := $burg[data(./burgemeester) = $j]
        where not(empty($g))
        return <entry>
                    <geslacht>{
                        data($g/sex)
                    }</geslacht>
                    <partij>{
                        data($g/partij)
                    }</partij>
                    <burgemeester>
                    {
                       if (not(empty($b))) then 'Y' else 'N'
                    }
                    </burgemeester>
                    <naam>{
                        $j
                    }</naam>
                    <aantal>{
                        count($d)
                    }</aantal>
               </entry>}
           </onderbrekerstotalen>
 };
 
 declare function onderbrekings:maakData($collect,$onderbrekingsDataFile,$onderbrekingsTotalenFile,$onderbrokenDoorFile,$perPartijFile){
 let $c := onderbrekings:maakonderbrekersdata($collect)
 let $collection := 'xmldb:exist:///db/projdb'
 let $store-return-status := xmldb:store($collection, $onderbrekingsDataFile, $c)
 let $store-return-status1 := xmldb:store($collection, $onderbrekingsTotalenFile, onderbrekings:maakonderbrekerstotalen($c))
 let $store-return-status2 := xmldb:store($collection, $perPartijFile,  onderbrekings:onderpercperpartij($c))
 let $store-return-status3 := xmldb:store($collection, $onderbrokenDoorFile,  onderbrekings:onderbrokendoorperpercpartij($c))
 return <message>New Document Created {$store-return-status} at {$collection}/{$onderbrekingsDataFile}<br/> 
 New Document Created {$store-return-status1} at {$collection}/{$onderbrekingsTotalenFile}<br/> 
  New Document Created {$store-return-status2} at {$collection}/{$perPartijFile}<br/> 
  New Document Created {$store-return-status3} at {$collection}/{$onderbrokenDoorFile}<br/> 
 </message>
 };